package org.budo.html.editor.kind.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.budo.html.editor.filter.BudoHtmlEditorFilter;
import org.budo.html.editor.handler.AbstractBudoHtmlEditorHandler;
import org.budo.support.dao.page.Page;
import org.budo.support.dao.page.PageModel;
import org.budo.support.lang.util.IoUtil;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.mvcs.Mvcs;
import org.budo.time.Time;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

/**
 * @author lmw
 */
public class KindEditorHandler extends AbstractBudoHtmlEditorHandler {
    @Override
    public Boolean handleRequest(HttpServletRequest request, HttpServletResponse response, BudoHtmlEditorFilter budoHtmlEditorFilter) {
        String requestUri = request.getRequestURI();

        if (!requestUri.contains("/kind/")) {
            return false;
        }

        if (requestUri.contains("/kind/uploadJson")) {
            this.uploadJson(request, response, budoHtmlEditorFilter);
        }

        if (requestUri.contains("/kind/fileManagerJson")) {
            this.fileManagerJson(request, response, budoHtmlEditorFilter);
        }

        if (requestUri.contains("/kind/resources/")) {
            String resourceName = requestUri.replace("/budo-html-editor/kind/resources/", "/META-INF/resources/kind/");
            return this.writeClassPathResource(resourceName, response);
        }

        return true;
    }

    private void fileManagerJson(HttpServletRequest request, HttpServletResponse response, BudoHtmlEditorFilter budoHtmlEditorFilter) {
        Page page = new Page().setPageSize(100).setOffset(0);
        String order = request.getParameter("order");
        if ("NAME".equals(order)) {
            page.setOrderBy(" file_name ASC , id DESC ");
        }
        if ("TYPE".equals(order)) {
            page.setOrderBy(" content_type ASC , id DESC ");
        }

        PageModel<String> urls = budoHtmlEditorFilter.listFile(page);

        List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>();

        for (String url : urls.getList()) {
            Map<String, Object> map = MapUtil.stringObjectMap("is_dir", false, //
                    "has_file", false, //
                    "filesize", 1, //
                    "is_photo", true, //
                    "filetype", "png", //
                    "filename", url, //
                    "datetime", Time.now().toString("yyyy-MM-dd HH:mm:ss"));

            fileList.add(map);
        }

        Map<String, Object> map = MapUtil.stringObjectMap("moveup_dir_path", "", //
                "current_dir_path", "", //
                "current_url", "", //
                "total_count", "", //
                "file_list", fileList);
        IoUtil.write(JSON.toJSONString(map), Mvcs.getOutputStream(response));
    }

    private void uploadJson(HttpServletRequest request, HttpServletResponse response, BudoHtmlEditorFilter budoHtmlEditorFilter) {
        MultipartFile multipartFile = this.getFirstMultipartFile(request);
        String url = budoHtmlEditorFilter.handleMultipartFile(multipartFile);

        Map<String, Object> map = MapUtil.stringObjectMap("error", 0, "url", url);
        IoUtil.write(JSON.toJSONString(map), Mvcs.getOutputStream(response));
    }
}